/*
 * IGSQLIndiceBursatilDAO.java
 *
 * Created on 31 de octubre de 2007, 16:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package webportlet.model.indiceBursatil.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import webportlet.model.indiceBursatil.dto.*;
import webportlet.model.util.*;

/**
 *
 * @author Administrador
 */
public class IGSQLIndiceBursatilDAO extends AbstractSQLIndiceBursatilDAO {
    
     public IndiceBursatilDTO create(Connection connection, IndiceBursatilDTO indiceBursatilDTO) 
        throws DuplicateInstanceException, InternalErrorException{

        PreparedStatement preparedStatement = null;
    
        try {
            /*Get Consejo Identifier. */
            EntityIdentifierGenerator entityIdentifierGenerator =
                    EntityIdentifierGeneratorFactory.getGenerator();
            Long id = entityIdentifierGenerator.nextIdentifier(connection,
                    "IndiceBursatil");
            
            /* Create "preparedStatement". */
            String queryString = "INSERT INTO Consejo" +
                " (idIndice,empresa, valor)" +
                " VALUES (?, ?, ?)";
            preparedStatement = connection.prepareStatement(queryString);    
            
            /* Fill "preparedStatement". */
            int i = 1;
            preparedStatement.setLong(i++, id);
            preparedStatement.setString(i++, indiceBursatilDTO.getEmpresa());
            preparedStatement.setDouble(i++, indiceBursatilDTO.getValor());
                        
            /* Execute query. */
            int insertedRows = preparedStatement.executeUpdate();
        
            if (insertedRows == 0) {
                throw new SQLException("Can not add row to table" +
                    " 'Indice'");
            }

            if (insertedRows > 1) {
                throw new SQLException("Duplicate row for idConsejo = '" + 
                    indiceBursatilDTO.getId() + "' in table 'IndiceBursatil'");
            }
            
            return new IndiceBursatilDTO(new Long(id), indiceBursatilDTO.getEmpresa(),
                    indiceBursatilDTO.getValor());
        
        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeStatement(preparedStatement);
        }  
    }
    
     
   
    
}
